﻿Imports System.Data.OleDb
Public Class frmThemMucTruHoaDon
#Region "Form Event"
    Public MucTruHoaDonId As Integer = -1
    Private Sub frmThemMucTruHoaDon_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If MucTruHoaDonId <> -1 Then
                Dim dbConn As New OleDb.OleDbConnection
                Dim dataAccess As New DataAccess
                Dim dsNhanVienTiepThi As New DataSet
                Dim dtNhanVienTiepThi As New DataTable
                Dim query As String = "Select * From MucTruHoaDon Where MucTruHoaDonId=" & MucTruHoaDonId
                Dim adapter As OleDb.OleDbDataAdapter
                dataAccess.ConnectDatabase_DBAccess(dbConn)
                adapter = New OleDb.OleDbDataAdapter(query, dbConn)
                adapter.Fill(dsNhanVienTiepThi)
                dbConn.Close()
                dtNhanVienTiepThi = dsNhanVienTiepThi.Tables(0)
                txtKhoanTruHoaDon.Text = dtNhanVienTiepThi.Rows(0)("TenMucTru")
                If dtNhanVienTiepThi.Rows(0)("TheoPhanTram") = "Yes" Then
                    txtTru.Text = dtNhanVienTiepThi.Rows(0)("Tru")
                    cbxIsPercent.Checked = True
                Else
                    txtTru.Text = dtNhanVienTiepThi.Rows(0)("Tru")
                    cbxIsPercent.Checked = False
                End If

                txtGhiChu.Text = dtNhanVienTiepThi.Rows(0)("GhiChu")
            End If
        Catch ex As Exception
            MessageBox.Show(ex.InnerException.ToString)
        End Try
    End Sub
#End Region
#Region "Button Event"

    Private Sub txtKhoanTruHoaDon_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtKhoanTruHoaDon.KeyUp
        Try
            txtKhoanTruHoaDon.BackColor = Color.White
        Catch ex As Exception

        End Try
    End Sub

    'Private Sub txtTru_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
    '    If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) Then
    '        e.Handled = True
    '    End If
    'End Sub
    'Private Sub txtTru_Leave(ByVal sender As Object, ByVal e As System.EventArgs)
    '    Try
    '        If Not cbxIsPercent.Checked Then
    '            If txtTru.Text <> "" Then
    '                txtTru.Text = txtTru.Text.Replace(",", "").Replace(".", "")
    '                txtTru.Text = CDec(txtTru.Text).ToString("N", Globalization.CultureInfo.CreateSpecificCulture("vi-vn"))
    '            End If
    '        Else
    '            txtTru.Text = txtTru.Text.Replace(",", "").Replace(".", "")
    '        End If
    '    Catch ex As Exception

    '    End Try
    'End Sub
    'Private Sub txtTru_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
    '    Try
    '        txtTru.BackColor = Color.White
    '    Catch ex As Exception
    '        ShowError()
    '    End Try
    'End Sub

    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Try
            If txtKhoanTruHoaDon.Text = "" Then
                EmptyData(txtKhoanTruHoaDon)
                Exit Sub
            End If
            If txtTru.Text = "" Then
                txtTru.Focus()
                Exit Sub
            End If
            If cbxIsPercent.Checked Then
                If Single.Parse(txtTru.Text) > 100 Then
                    MessageBox.Show("Khoản trừ phần trăm hóa đơn không hợp lý.Vui lòng kiểm tra lại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Exit Sub
                End If
            End If

            Dim dataAccess As New DataAccess
            Dim dbConn As New OleDb.OleDbConnection
            Dim dbCommand As OleDbCommand
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            Dim query As String = ""
            Dim TheoPhanTram As String
            If cbxIsPercent.Checked Then
                TheoPhanTram = "Yes"
            Else
                TheoPhanTram = "No"
            End If
            If MucTruHoaDonId = -1 Then
                query = String.Format("Insert Into MucTruHoaDon(TenMucTru,Tru,TheoPhanTram,GhiChu) Values(N'{0}',{1},N'{2}',N'{3}')", txtKhoanTruHoaDon.Text, Single.Parse(txtTru.Text, Globalization.NumberStyles.Currency), TheoPhanTram, txtGhiChu.Text)
            Else
                query = String.Format("Update MucTruHoaDon Set TenMucTru=N'{0}',Tru={1},TheoPhanTram=N'{2}',GhiChu=N'{3}' Where MucTruHoaDonId={4}", txtKhoanTruHoaDon.Text, Single.Parse(txtTru.Text, Globalization.NumberStyles.Currency), TheoPhanTram, txtGhiChu.Text, MucTruHoaDonId)
            End If
            dbCommand = New OleDbCommand(query, dbConn)
            dbCommand.ExecuteNonQuery()
            dbConn.Close()
            txtKhoanTruHoaDon.Text = ""
            txtTru.Text = "0.00"
            txtGhiChu.Text = ""
            frmMucTruHoaDon.MucTruHoaDon_Load()
            If MucTruHoaDonId <> -1 Then
                Me.Close()
            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
        Try
            txtKhoanTruHoaDon.Text = ""
            txtTru.Text = "0.00"
            txtGhiChu.Text = ""
            frmMucTruHoaDon.MucTruHoaDon_Load()
            Me.Close()
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#End Region
#Region "Load Data"

#End Region

    Private Sub cbxIsPercent_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxIsPercent.CheckedChanged
        'If Not cbxIsPercent.Checked Then
        '    '--->
        '    If txtTru.Text <> "" Then
        '        'txtTru.Text = txtTru.Text.Split(",")(0)
        '        'txtTru.Text = txtTru.Text.Replace(",", "").Replace(".", "")
        '        txtTru.Text = CDec(txtTru.Text).ToString("N", Globalization.CultureInfo.CreateSpecificCulture("vi-vn"))
        '    End If
        'Else
        '    '--->Theo phần trăm
        '    txtTru.Text = txtTru.Text.Split(",")(0).Replace(".", "")
        'End If
    End Sub
End Class